package com.rgs.module.login.controller;

import com.rgs.base.core.model.WebApiReturnResultModel;
import com.rgs.base.core.system.WebResponseState;
import com.rgs.module.login.bo.LoginUserBO;
import com.rgs.module.login.dto.request.LoginRequestDTO;
import com.rgs.module.login.dto.request.UserLoginRequestDTO;
import com.rgs.module.login.service.LoginCacheService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @author Ren Gaoshuai
 * @ClassName LoginController
 * @date 2021-12-24 16:17
 **/
@RestController
@RequestMapping("/v1/api")
@Api(tags = "登录")
@Slf4j
public class LoginController {

    @Resource
    AuthenticationManager authenticationManager;
    @Resource
    LoginCacheService loginCacheService;

    private static final String TEST_CAPTCHA = "1111";

    @PostMapping("/login")
    public WebApiReturnResultModel<String> login(@RequestBody @Validated LoginRequestDTO loginRequest) {
        //验证用户名密码
        Authentication authenticate = authenticationManager.authenticate(
                new UsernamePasswordAuthenticationToken(
                        loginRequest.getUsername(),
                        loginRequest.getPassword()));

        //得到security返回的对象
        LoginUserBO loginUser = (LoginUserBO) authenticate.getPrincipal();

        //生成对象唯一token
        String token = loginCacheService.createToken(loginUser);
        return WebApiReturnResultModel.ofSuccess(token);
    }


    @ApiOperation(value = "后台用户登录（带验证码）")
    @PostMapping("/user/login")
    public WebApiReturnResultModel<String> login(@RequestBody UserLoginRequestDTO userLoginRequest) {

        //验证码
        if (!TEST_CAPTCHA.equals(userLoginRequest.getCode())) {
            if (!loginCacheService.checkCaptcha(userLoginRequest.getUuid(), userLoginRequest.getCode())) {
                return WebApiReturnResultModel.of(WebResponseState.INTERNAL_SERVER_ERROR, "验证码错误", "");
            }
        }
        Authentication authenticate;
        //验证用户名密码
        try {
            authenticate = authenticationManager.authenticate(
                    new UsernamePasswordAuthenticationToken(
                            userLoginRequest.getUsername(),
                            userLoginRequest.getPassword()));
        } catch (BadCredentialsException e) {
            return WebApiReturnResultModel.of(WebResponseState.PASSWORD_ERROR,
                    WebResponseState.PASSWORD_ERROR.getStandardMessage(), null);
        }

        //得到security返回的对象
        LoginUserBO loginUser = (LoginUserBO) authenticate.getPrincipal();

        //生成对象唯一token
        String token = loginCacheService.createToken(loginUser);
        return WebApiReturnResultModel.ofSuccess(token);
    }
}
